package com.windchime.blog.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windchime.blog.entity.User;
import com.windchime.blog.entity.vo.UserFrontVo;
import com.windchime.blog.entity.vo.UserSearch;
import com.windchime.blog.mapper.UserMapper;
import com.windchime.blog.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author WindChime
 * @since 2021-07-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 保存用户
     * @param user 用户
     */
    @Override
    public boolean saveUser(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        User has_user = baseMapper.selectOne(wrapper);
        if (has_user == null) {
            user.setPassword(SecureUtil.md5(user.getPassword()));
            return baseMapper.insert(user) > 0;
        } else {
            return false;
        }
    }

    /**
     * 修改用户
     * @param user 用户
     * @return true
     */
    @Override
    public boolean editUser(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        //查询用户表的所有用户，根据ID去掉传进来的用户，在把这个用户的用户名变成list集合
        wrapper.notIn("id", user.getId());
        List<User> tags = baseMapper.selectList(wrapper);
        List<String> userNames = tags.stream().map(User::getUsername).collect(Collectors.toList());

        User old_user = baseMapper.selectById(user.getId());
        if (!old_user.getPassword().equals(user.getPassword())) {
            user.setPassword(SecureUtil.md5(user.getPassword()));
        }

        //判断传入过来的用户名有没有占数据库中除自己外的用户名
        if (!userNames.contains(user.getUsername())) {
            return baseMapper.updateById(user) > 0;
        }

        return false;
    }

    /**
     * 带条件的分页查询
     * @param page       当前页
     * @param limit      每页数量
     * @param userSearch 条件
     * @return list
     */
    @Override
    public PageInfo<User> selectUserList(int page, int limit, UserSearch userSearch) {
        PageHelper.startPage(page, limit);
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.orderByDesc("create_time");

        if (!StringUtils.isEmpty(userSearch.getUsername())) {
            wrapper.like("username", userSearch.getUsername());
        }
        if (!StringUtils.isEmpty(userSearch.getSex())) {
            wrapper.eq("sex", userSearch.getSex());
        }
        List<User> users = baseMapper.selectList(wrapper);
        return new PageInfo<>(users);
    }

    /**
     * 前台用户登录
     * @param userFrontVo 对象
     * @return user
     */
    @Override
    public User userLogin(UserFrontVo userFrontVo) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", userFrontVo.getUsername());
        wrapper.eq("password", SecureUtil.md5(userFrontVo.getPassword()));
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 前台用户注册
     * @param userFrontVo 对象
     * @return true
     */
    @Override
    public boolean userRegister(UserFrontVo userFrontVo) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        User user = new User();
        userFrontVo.setPassword(SecureUtil.md5(userFrontVo.getPassword()));
        BeanUtils.copyProperties(userFrontVo, user);
        wrapper.eq("username", userFrontVo.getUsername());
        User has_user = baseMapper.selectOne(wrapper);
        if (has_user == null) {
            return baseMapper.insert(user) > 0;
        } else {
            return false;
        }
    }
}
